{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Special Positions in a Binary Matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #matrix"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: numSpecial"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #二进制矩阵中的特殊位置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个大小为 <code>rows x cols</code> 的矩阵 <code>mat</code>，其中 <code>mat[i][j]</code> 是 <code>0</code> 或 <code>1</code>，请返回 <strong>矩阵&nbsp;<em><code>mat</code></em> 中特殊位置的数目</strong> 。</p>\n",
    "\n",
    "<p><strong>特殊位置</strong> 定义：如果 <code>mat[i][j] == 1</code> 并且第 <code>i</code> 行和第 <code>j</code> 列中的所有其他元素均为 <code>0</code>（行和列的下标均 <strong>从 0 开始</strong> ），则位置 <code>(i, j)</code> 被称为特殊位置。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>mat = [[1,0,0],\n",
    "&nbsp;           [0,0,<strong>1</strong>],\n",
    "&nbsp;           [1,0,0]]\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>(1,2) 是一个特殊位置，因为 mat[1][2] == 1 且所处的行和列上所有其他元素都是 0\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>mat = [[<strong>1</strong>,0,0],\n",
    "&nbsp;           [0,<strong>1</strong>,0],\n",
    "&nbsp;           [0,0,<strong>1</strong>]]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>(0,0), (1,1) 和 (2,2) 都是特殊位置\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>mat = [[0,0,0,<strong>1</strong>],\n",
    "&nbsp;           [<strong>1</strong>,0,0,0],\n",
    "&nbsp;           [0,1,1,0],\n",
    "&nbsp;           [0,0,0,0]]\n",
    "<strong>输出：</strong>2\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>mat = [[0,0,0,0,0],\n",
    "&nbsp;           [<strong>1</strong>,0,0,0,0],\n",
    "&nbsp;           [0,<strong>1</strong>,0,0,0],\n",
    "&nbsp;           [0,0,<strong>1</strong>,0,0],\n",
    "&nbsp;           [0,0,0,1,1]]\n",
    "<strong>输出：</strong>3\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>rows == mat.length</code></li>\n",
    "\t<li><code>cols == mat[i].length</code></li>\n",
    "\t<li><code>1 &lt;= rows, cols &lt;= 100</code></li>\n",
    "\t<li><code>mat[i][j]</code> 是 <code>0</code> 或 <code>1</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [special-positions-in-a-binary-matrix](https://leetcode.cn/problems/special-positions-in-a-binary-matrix/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [special-positions-in-a-binary-matrix](https://leetcode.cn/problems/special-positions-in-a-binary-matrix/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[[1,0,0],[0,0,1],[1,0,0]]', '[[1,0,0],[0,1,0],[0,0,1]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m, n, ans = len(mat[1]), len(mat), 0  # 分别是列数，行数，和记录特殊位置的数量\n",
    "        for i in range(n):\n",
    "            if sum(mat[i])==1:\n",
    "                for j in range(m):\n",
    "                    if sum(mat[e][j] for e in range(n))==1:\n",
    "                        if mat[i][j]==1:\n",
    "                            ans +=1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        #copy\n",
    "        return sum(mat[r][c]  for r,c in product(rows,cols)) if (rows :=[i for i in range(len(mat)) if sum(mat[i]) ==1])  and (cols :=[i for i ,col in enumerate(zip(*mat)) if sum(col)==1]) else 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        x = set(i for i, l in enumerate(zip(*mat)) if l.count(1) == 1)\n",
    "        return sum(l.count(1) == 1 and l.index(1) in x for l in mat)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    # 优化点，记录不满足的行列，进行剪枝处理\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows, cols = mat.__len__(), mat[0].__len__()\n",
    "        ans = 0\n",
    "        for i in range(rows):\n",
    "            for j in range(cols):\n",
    "                if mat[i][j] != 1:\n",
    "                    continue\n",
    "                # 校验行\n",
    "                if sum(mat[i]) != 1:\n",
    "                    continue\n",
    "                # 校验列\n",
    "                col_sum = 0\n",
    "                for index in range(rows):\n",
    "                    col_sum += mat[index][j]\n",
    "                if col_sum != 1:\n",
    "                    continue\n",
    "                # 均符合预期，数值+1\n",
    "                ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        for i in range(len(mat)):\n",
    "            line = mat[i]\n",
    "            if line.count(1) == 1:\n",
    "                for j in range(len(line)):\n",
    "                    if mat[i][j] == 1 and [subarray[j] for subarray in mat].count(1) == 1:\n",
    "                        print(\"%d, %d\" % (i, j))\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        count_col = {}\n",
    "        count_row = {}\n",
    "        for i in range(len(mat)):\n",
    "            count_col[i] = mat[i].count(1) == 1\n",
    "        for j in range(len(mat[0])):\n",
    "            count_row[j] = [subarray[j] for subarray in mat].count(1) == 1\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                if mat[i][j] == 1 and count_col[i] and count_row[j]:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        mat_T = [i for i in zip(*mat)]\n",
    "        res = 0\n",
    "        for i in mat:\n",
    "            if sum(i) == 1:\n",
    "                for j in range(len(i)):\n",
    "                    if i[j] == 1 and sum(mat_T[j]) == 1:\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows = len(mat)\n",
    "        cols = len(mat[0])\n",
    "        x,y,i,j,n,nums = 0,0,0,0,0,0\n",
    "        while i < rows:\n",
    "            n = 0\n",
    "            y = 0\n",
    "            if mat[i][j] == 1:    \n",
    "                while n < rows:\n",
    "                    y += mat[n][j]\n",
    "                    n += 1\n",
    "                x = sum(m for m in mat[i]) + y\n",
    "                if x == 2:\n",
    "                    nums += 1\n",
    "            if j < cols - 1 :\n",
    "                j += 1\n",
    "            elif j == cols - 1:\n",
    "                i += 1\n",
    "                j = 0         \n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        n=len(mat)\n",
    "        m=len(mat[0])\n",
    "        ans=0\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "               if mat[i][j]==1 and sum(mat[i])+sum(row[j] for row in mat)-1 ==1:\n",
    "                   ans +=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        r = len(mat)\n",
    "        ans = 0\n",
    "        for i in range(r):\n",
    "            if mat[i].count(1) == 1:\n",
    "                idx = mat[i].index(1)\n",
    "                n = sum(1 for j in range(r) if mat[j][idx] == 1)\n",
    "                if n == 1:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows = len(mat)\n",
    "        cols = len(mat[0])\n",
    "        x,y,i,j,n,nums = 0,0,0,0,0,0\n",
    "        while i < rows:\n",
    "            n = 0\n",
    "            y = 0\n",
    "            if mat[i][j] == 1:    \n",
    "                while n < rows:\n",
    "                    y += mat[n][j]\n",
    "                    n += 1\n",
    "                x = sum(m for m in mat[i]) + y\n",
    "                if x == 2:\n",
    "                    nums += 1\n",
    "            if j < cols - 1 :\n",
    "                j += 1\n",
    "            elif j == cols - 1:\n",
    "                i += 1\n",
    "                j = 0         \n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rowSum = [sum(x) for x in mat]\n",
    "        colSum = [sum(x) for x in zip(*mat)]\n",
    "        Spe = 0\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                if mat[i][j] == 1:\n",
    "                    if rowSum[i] == 1 and colSum[j] == 1:\n",
    "                        Spe += 1\n",
    "        return Spe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        rows = [sum(r) for r in mat]\n",
    "        cols = [0]*n\n",
    "        for j in range(n):\n",
    "            cols[j] = sum([mat[r][j] for r in range(m)])\n",
    "\n",
    "        ans = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if mat[i][j] == 1 and rows[i] == 1 and cols[j] == 1:\n",
    "                    ans += 1\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                if mat[i][j] != 1:\n",
    "                    continue\n",
    "                row_val = sum(mat[i])\n",
    "                col_val = 0\n",
    "                for h in range(len(mat)):\n",
    "                    col_val += mat[h][j]\n",
    "                if row_val == 1 and col_val == 1:\n",
    "                    res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        sr=[sum(r) for r in mat]\n",
    "        sc=[sum(c) for c in zip(*mat)]\n",
    "        return sum(mat[i][j]==1 and sr[i]==1 and sc[j]==1 for j in range(len(mat[0])) for i in range(len(mat)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows_sum = [sum(row) for row in mat]\n",
    "        cols_sum = [sum(col) for col in zip(*mat)]\n",
    "        res = 0\n",
    "        for i, row in enumerate(mat):\n",
    "            for j, x in enumerate(row):\n",
    "                if x == 1 and rows_sum[i] == 1 and cols_sum[j] == 1:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        sum_row = [sum(row) for row in mat]\n",
    "\n",
    "        sum_col = []\n",
    "        for j in range(n):\n",
    "            cur_sum = 0\n",
    "            for i in range(m):\n",
    "                cur_sum += mat[i][j]\n",
    "            sum_col.append(cur_sum)\n",
    "\n",
    "        res = 0 \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if sum_row[i] == 1 and sum_col[j] == 1 and mat[i][j] == 1:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        cols = [sum(num) for num in mat]\n",
    "        rows = [sum(num) for num in zip(*mat)]\n",
    "        return sum(rows[mat[i].index(1)]==1 for i in range(len(cols)) if cols[i]==1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat) -> int:\n",
    "        x_row = [row.count(1) for row in mat]\n",
    "        y_col = [col.count(1) for col in zip(*mat)]\n",
    "        \n",
    "        count = 0\n",
    "        for i,row in enumerate(mat):\n",
    "            for j,x in enumerate(row):\n",
    "                if x == 1 and x_row[i] == 1 and y_col[j] == 1:\n",
    "                    count += 1\n",
    "                    \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        return sum(mat[r][c] for r, c in product(rows, cols)) if (rows := [i for i in range(len(mat)) if sum(mat[i]) == 1 ]) and (cols := [i for i, col in enumerate(zip(*mat)) if sum(col) == 1]) else 0\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m=len(mat)\n",
    "        coun_row=[sum(row) for row in mat]\n",
    "        coun_col=[sum(col) for col in zip(*mat)]\n",
    "        ans=0\n",
    "        for i in range(m):\n",
    "            if coun_row[i]==1:\n",
    "                j=mat[i].index(1)\n",
    "                if coun_col[j]==1:\n",
    "                    ans+=1 \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        count_col = {}\n",
    "        count_row = {}\n",
    "\n",
    "        for i in range(len(mat)):\n",
    "            count_col[i] = mat[i].count(1) == 1\n",
    "        for j in range(len(mat[0])):\n",
    "            count_row[j] = [subarray[j] for subarray in mat].count(1) == 1\n",
    "\n",
    "        met_col = set()\n",
    "        met_row = set()\n",
    "        for i in range(len(mat)):\n",
    "            if i in met_col:\n",
    "                continue\n",
    "            for j in range(len(mat[0])):\n",
    "                if j in met_row:\n",
    "                    continue\n",
    "                if mat[i][j] == 1 and count_col[i] and count_row[j]:\n",
    "                    met_col.add(i)\n",
    "                    met_row.add(j)\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        M = [0]*m\n",
    "        N = [0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if mat[i][j] == 1:\n",
    "                    M[i] += 1\n",
    "                    N[j] += 1\n",
    "        特殊位置数目 = 0\n",
    "        for i in range(m):\n",
    "            if M[i] == 1:\n",
    "                for j in range(n):\n",
    "                    if mat[i][j] == 1 and N[j] == 1:\n",
    "                        特殊位置数目 += 1\n",
    "                        break\n",
    "        return 特殊位置数目\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "\n",
    "\n",
    "        for i, row in enumerate(mat):\n",
    "            cnt1 = sum(row) - (i==0)\n",
    "            if cnt1:\n",
    "                for j, x in enumerate(row):\n",
    "                    if x == 1:\n",
    "                        mat[0][j] += cnt1\n",
    "\n",
    "\n",
    "        return sum(x==1 for x in mat[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        count_col = {}\n",
    "        count_row = {}\n",
    "\n",
    "        for i in range(len(mat)):\n",
    "            count_col[i] = mat[i].count(1) == 1\n",
    "        for j in range(len(mat[0])):\n",
    "            count_row[j] = [subarray[j] for subarray in mat].count(1) == 1\n",
    "\n",
    "        met_col = set()\n",
    "        met_row = set()\n",
    "        for i in range(len(mat)):\n",
    "            if i in met_col:\n",
    "                continue\n",
    "            for j in range(len(mat[0])):\n",
    "                if j in met_row:\n",
    "                    continue\n",
    "                if mat[i][j] == 1 and count_col[i] and count_row[j]:\n",
    "                    met_col.add(i)\n",
    "                    met_row.add(j)\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m,n=len(mat),len(mat[0])\n",
    "        rows=[sum([mat[i][j]for j in range(n)]) for i in range(m)]\n",
    "        columns=[sum([mat[i][j]for i in range(m)]) for j in range(n)]\n",
    "        count=0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if mat[i][j]==1 and rows[i]==1 and columns[j]==1:\n",
    "                    count+=1\n",
    "        return count\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        def isSpecial(i, j):\n",
    "            for y in range(n):\n",
    "                if y != j and mat[i][y] == 1:\n",
    "                    return False\n",
    "            for x in range(m):\n",
    "                if x != i and mat[x][j] == 1:\n",
    "                    return False\n",
    "            return True            \n",
    "                    \n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if mat[i][j] == 1 and isSpecial(i, j):\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        m = len(mat[0])\n",
    "        for i in range(n):\n",
    "            cnt = 0\n",
    "            for j in range(m):\n",
    "                if mat[i][j] == 1:\n",
    "                    cnt += 1\n",
    "            if i == 0:\n",
    "                cnt -= 1\n",
    "            if cnt > 0:\n",
    "                for j in range(m):\n",
    "                    if mat[i][j] == 1:\n",
    "                        mat[0][j] += cnt\n",
    "        sum = 0\n",
    "        for i in range(m):\n",
    "            if mat[0][i] == 1:\n",
    "                sum += 1\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        mat_T = [sum(i) for i in zip(*mat)]\n",
    "        res = 0\n",
    "        for i in mat:\n",
    "            if sum(i) == 1:\n",
    "                for j in range(len(i)):\n",
    "                    if i[j] == 1 and mat_T[j] == 1:\n",
    "                        res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        b=0\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                if mat[i][j]==1:\n",
    "                    a=0\n",
    "                    for k in range(len(mat)):\n",
    "                        a+=mat[k][j]\n",
    "                    if sum(mat[i])==1 and a==1:\n",
    "                        b+=1\n",
    "        return b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows_sum = [sum(row) for row in mat]\n",
    "        cols_sum = [sum(col) for col in zip(*mat)]\n",
    "        res = 0\n",
    "        for i, row in enumerate(mat):\n",
    "            for j, x in enumerate(row):\n",
    "                if x == 1 and rows_sum[i] == 1 and cols_sum[j] == 1:\n",
    "                    res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m, n, ans = len(mat[1]), len(mat), 0  # 分别是列数，行数，和记录特殊位置的数量\n",
    "        for i in range(n):  # 从第一行开始\n",
    "            for j in range(m):  # 从第一列开始\n",
    "                if mat[i][j] == 1 and sum(mat[i]) == 1:  # 如果当前位置是1且这一行的和也是1，再进行下一步判断，否则pass\n",
    "                    if sum([mat[e][j] for e in range(n) if e != i]) == 0:  # 再计算这一列其他值得和是否为0（也可以不加‘if e != i’，这样就判断和是否为1了）\n",
    "                        ans += 1\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        xrow = [row.count(1) for row in mat]\n",
    "        ycol = [col.count(1) for col in zip(*mat)]\n",
    "        count = 0\n",
    "        for i,row in enumerate(mat):\n",
    "            for j,x in enumerate(row):\n",
    "                if x==1 and xrow[i] == 1 and ycol[j] == 1:\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rowsSum = [sum(row) for row in mat]\n",
    "        colsSum = [sum(col) for col in zip(*mat)]\n",
    "        ans = 0\n",
    "        for i in range(len(mat)):\n",
    "            if rowsSum[i] == 1:\n",
    "                for j in range(len(mat[0])):\n",
    "                    if mat[i][j]==1 and colsSum[j] == 1:\n",
    "                        ans +=1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        mat_a = list(zip(*mat))         # 转置矩阵\n",
    "        ans = 0\n",
    "        for r in range(m):\n",
    "            if mat[r].count(1) == 1:\n",
    "                i = mat[r].index(1)\n",
    "                if mat[r][i] == 1 and mat_a[i].count(1) == 1:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows_sum = [sum(row) for row in mat]\n",
    "        cols_sum = [sum(col) for col in zip(*mat)]\n",
    "        res = 0\n",
    "        for i, row in enumerate(mat):\n",
    "            for j, x in enumerate(row):\n",
    "                if x == 1 and rows_sum[i] == 1 and cols_sum[j] == 1:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        cols = list(zip(*mat))\n",
    "        ans = 0\n",
    "        for rows in mat:\n",
    "            if sum(rows) == 1:\n",
    "                j = rows.index(1)\n",
    "                if sum(cols[j]) == 1:\n",
    "                    ans += 1\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        for i, row in enumerate(mat):\n",
    "            cnt1 = sum(row) - (i == 0)\n",
    "            if cnt1:\n",
    "                for j, x in enumerate(row):\n",
    "                    if x == 1:\n",
    "                        mat[0][j] += cnt1\n",
    "        return sum(x == 1 for x in mat[0])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        res = 0\n",
    "        mat1 = [[mat[i][j] for i in range(len(mat))] for j in range(len(mat[0]))]\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                if mat[i][j]==1:\n",
    "                    if mat[i].count(1)==1 and mat1[j].count(1)==1:\n",
    "                        res = res+1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        mat_a = list(zip(*mat))\n",
    "        ans = 0\n",
    "        for r in range(m):\n",
    "            if mat[r].count(1) == 1:\n",
    "                for c in range(n):\n",
    "                    if mat[r][c] == 1 and mat_a[c].count(1) == 1:\n",
    "                        ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        n = len(mat)\n",
    "        m = len(mat[0])\n",
    "\n",
    "        r = [0]*n\n",
    "        c = [0]*m\n",
    "\n",
    "\n",
    "        for i in range(n):\n",
    "            r[i] = sum(mat[i])\n",
    "\n",
    "        for j in range(m):\n",
    "            for i in range(n):\n",
    "                c[j] += mat[i][j]\n",
    "        \n",
    "        ans = 0\n",
    "        for i in range(n):\n",
    "            for j in range(m):\n",
    "                if r[i] == 1 and c[j] == 1 and mat[i][j] == 1:\n",
    "                    ans += 1\n",
    "        return ans\n",
    "\n",
    "\n",
    "# [[0,0,0,0,0,1,0,0], 1\n",
    "#  [0,0,0,0,1,0,0,1], 2\n",
    "#  [0,0,0,0,1,0,0,0], 1\n",
    "#  [1,0,0,0,1,0,0,0], 2\n",
    "#  [0,0,1,1,0,0,0,0]] 2\n",
    "#   1 0 1 0 3 1 0 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        r = len(mat)\n",
    "        c = len(mat[0])\n",
    "        rlist = [100] * len(mat)\n",
    "        clist = [100] * len(mat[0])\n",
    "        for i in range(r):\n",
    "            if sum(mat[i]) == 1:\n",
    "                rlist[i] = i\n",
    "        for j in range(c):\n",
    "            ans = 0\n",
    "            for i in range(r):\n",
    "                ans += mat[i][j]\n",
    "            if ans == 1:\n",
    "                clist[j] = j\n",
    "        ans = 0\n",
    "        for i in range(r):\n",
    "            for j in range(c):\n",
    "                if clist[j]==j and rlist[i]==i and mat[i][j] == 1:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        arr1 = [0] * len(mat)\n",
    "        arr2 = [0] * len(mat[0])\n",
    "        for i in range(len(mat)):\n",
    "            for j in range(len(mat[0])):\n",
    "                if mat[i][j] == 1:\n",
    "                    arr1[i] += 1\n",
    "                    arr2[j] += 1\n",
    "\n",
    "        res = 0\n",
    "        for i in range(len(arr1)):\n",
    "            if arr1[i] == 1:\n",
    "                for j in range(len(arr2)):\n",
    "                    if arr2[j] == 1:\n",
    "                        if mat[i][j] == 1:\n",
    "                            res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m=len(mat)\n",
    "        n=len(mat[0])\n",
    "        resres=0\n",
    "        for i in range(m):\n",
    "            if sum(mat[i]) == 1:\n",
    "                k = mat[i].index(1)\n",
    "                res=0\n",
    "                for j in range(m):\n",
    "                    if mat[j][k] == 1 and j!=i:\n",
    "                        res=1\n",
    "                if res==0:\n",
    "                    resres += 1\n",
    "        return resres\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows_sum = [sum(row) for row in mat]\n",
    "        cols_sum = [sum(col) for col in zip(*mat)]\n",
    "        res = 0\n",
    "        for i, row in enumerate(mat):\n",
    "            for j, x in enumerate(row):\n",
    "                if x == 1 and rows_sum[i] == 1 and cols_sum[j] == 1:\n",
    "                    res += 1\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        row = [0] * m\n",
    "        col = [0] * n\n",
    "        count = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                col[j] += mat[i][j]\n",
    "            row[i] = sum(mat[i])\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if mat[i][j] == 1 and row[i] == 1 and col[j] == 1:\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        def transpose(matrix: list) -> list:\n",
    "            res = [[0]*len(matrix) for i in range(len(matrix[0]))]\n",
    "            for i in range(len(matrix)):\n",
    "                for j in range(len(matrix[0])):\n",
    "                    res[j][i] = matrix[i][j]\n",
    "            return res\n",
    "        mat1 = transpose(mat)\n",
    "        row,col = len(mat),len(mat1)\n",
    "        count = 0\n",
    "        for i in range(row):\n",
    "            for j in range(col):\n",
    "                if mat[i][j]==1 and sum(mat[i])==1 and sum(mat1[j])==1:\n",
    "                    count += 1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        res = 0\n",
    "        for i in range(m):\n",
    "            if sum(mat[i]) == 1:\n",
    "                j = mat[i].index(1)\n",
    "                col = [mat[k][j] for k in range(m)]\n",
    "                print(col)\n",
    "                if sum(col) == 1:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows = [0 for _ in range(len(mat))]\n",
    "        cols = [0 for _ in range(len(mat[0]))]\n",
    "        for i in range(len(mat)):\n",
    "            rows[i] = sum(mat[i])\n",
    "        for i in range(len(mat[0])):\n",
    "            temp = 0\n",
    "            for j in range(len(mat)):\n",
    "                temp += mat[j][i]\n",
    "            cols[i] = temp\n",
    "        res = 0\n",
    "        print(rows)\n",
    "        print(cols)\n",
    "        for i in range(len(rows)):\n",
    "            if rows[i] == 1:\n",
    "                for j in range(len(mat[0])):\n",
    "                    if mat[i][j] ==  1:\n",
    "                        if cols[j] == 1:\n",
    "                            res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows_sum = [sum(row) for row in mat]\n",
    "        cols_sum = [sum(col) for col in zip(*mat)]\n",
    "        ans = 0\n",
    "        for i, row in enumerate(mat):\n",
    "            for j, x in enumerate(row):\n",
    "                if x == 1 and rows_sum[i] == 1 and cols_sum[j] == 1:\n",
    "                    ans += 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m,n = len(mat),len(mat[0])\n",
    "        ans = 0\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if mat[i][j]==1 and sum(mat[i])==1:\n",
    "                    s = 0\n",
    "                    for row in range(m):\n",
    "                        s += mat[row][j]\n",
    "                    if s==1:\n",
    "                        ans+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m = len(mat)\n",
    "        n = len(mat[0])\n",
    "        M = [0]*m\n",
    "        N = [0]*n\n",
    "        for i in range(m):\n",
    "            for j in range(n):\n",
    "                if mat[i][j] == 1:\n",
    "                    M[i] += 1\n",
    "                    N[j] += 1\n",
    "        特殊位置数目 = 0\n",
    "        for i in range(m):\n",
    "            if M[i] == 1:\n",
    "                for j in range(n):\n",
    "                    if mat[i][j] == 1 and N[j] == 1:\n",
    "                        特殊位置数目 += 1\n",
    "                        break\n",
    "        return 特殊位置数目\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows_sum = [sum(row) for row in mat]\n",
    "        cols_sum = [sum(col) for col in zip(*mat)]\n",
    "        res = 0\n",
    "        for i, row in enumerate(mat):\n",
    "            for j, x in enumerate(row):\n",
    "                if x == 1 and rows_sum[i] == 1 and cols_sum[j] == 1:\n",
    "                    res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        r = len(mat)\n",
    "        c = len(mat[0])\n",
    "        ans = 0\n",
    "        for i in range(r):\n",
    "            if sum(mat[i][:]) == 1:                \n",
    "                for j in range(c):\n",
    "                    if mat[i][j] == 1:\n",
    "                        if sum(mat[x][j] for x in range(r)) == 1:\n",
    "                            ans += 1\n",
    "                        else:\n",
    "                            break        \n",
    "            \n",
    "        return ans        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat) -> int:\n",
    "        x_row = [row.count(1) for row in mat]\n",
    "        y_col = [col.count(1) for col in zip(*mat)]\n",
    "        \n",
    "        count = 0\n",
    "        for i,row in enumerate(mat):\n",
    "            for j,x in enumerate(row):\n",
    "                if x == 1 and x_row[i] == 1 and y_col[j] == 1:\n",
    "                    count += 1\n",
    "                    \n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows_sum = [sum(row) for row in mat]\n",
    "        cols_sum = [sum(col) for col in zip(*mat)]\n",
    "        res = 0\n",
    "        for i, row in enumerate(mat):\n",
    "            for j, x in enumerate(row):\n",
    "                if x == 1 and rows_sum[i] == 1 and cols_sum[j] == 1:\n",
    "                    res += 1\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        m=list(zip(*mat))\n",
    "        c=0\n",
    "        for i in range(len(mat)):\n",
    "            x=collections.Counter(mat[i])\n",
    "            if x[1]==1:\n",
    "                a=mat[i].index(1)\n",
    "                y=collections.Counter(m[a])\n",
    "                if y[1]==1:\n",
    "                    c+=1\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rownum=len(mat)\n",
    "        colnum=len(mat[0])\n",
    "        count=0\n",
    "        mat2=[[mat[i][j] for i in range(rownum)]for j in range(colnum)]\n",
    "        for i in range(rownum):\n",
    "            for j in range(colnum):\n",
    "                if mat[i][j]==1 and mat[i].count(1)==1 and mat2[j].count(1)==1:\n",
    "                    count+=1\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "\n",
    "        def checkCol(mat, m, pos):\n",
    "            return sum([mat[j][pos] for j in range(m)]) == 1\n",
    "        m, n = len(mat), len(mat[0])\n",
    "        ans = 0\n",
    "        # find rows\n",
    "        for i in range(m):\n",
    "            tmp_count = 0\n",
    "            pos = 0\n",
    "            for j in range(n):\n",
    "                tmp_count += mat[i][j]\n",
    "                pos = j if mat[i][j] == 1 else pos\n",
    "            if tmp_count == 1 and checkCol(mat, m, pos):\n",
    "                ans += 1\n",
    "        return ans\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        rows_sum = [sum(row) for row in mat]\n",
    "        cols_sum = [sum(col) for col in zip(*mat)]\n",
    "        ans = 0\n",
    "        print(rows_sum)\n",
    "        print(cols_sum)\n",
    "        for i, row in enumerate(mat):\n",
    "            for j,x in enumerate(row):\n",
    "                if x==1 and rows_sum[i]==1 and cols_sum[j]==1:\n",
    "                    ans+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def numSpecial(self, mat: List[List[int]]) -> int:\n",
    "        l1=[]\n",
    "        l2=[]\n",
    "        ans=0\n",
    "        for i in range(0,len(mat)):\n",
    "            for j in range(0,len(mat[0])):\n",
    "                if mat[i][j]==1:\n",
    "                    l1.append(i)\n",
    "                    l2.append(j)\n",
    "        for (i,j) in enumerate(l1):\n",
    "            if l1.count(j)==1 and l2.count(l2[i])==1:\n",
    "                ans=ans+1\n",
    "        return ans"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
